<?php
//模型
class AdminModel extends MY_Model {
	function __construct(){
		parent::__construct();
		$this->table = 'user';
	}
	
	
	function get_list($arr,$page=1,$size=10){
		$where = ' 1 ';
		
		if(isset($arr['nickname']) && !empty($arr['nickname'])){
			$where .= " AND nickname like '%{$arr['nickname']}%'";
		}
		
		$res = $this->_where_get_list($where,$page,$size,'order by id asc');
		return $res;
	}
	
	function get_info($id){
		$sql  = "select * from ".$this->db->dbprefix.$this->table." where id='{$id}' limit 1";
		$query = $this->db->query($sql);
		$row = $query->row_array();
		return $row;
	}
	
	function field_get_info($field,$val){
		if(!in_array($field,array('username'))){
			return false;
		}
		$sql  = "select * from ".$this->db->dbprefix.$this->table." where `{$field}`='{$val}' limit 1";
		
		$query = $this->db->query($sql);
		$row = $query->row_array();
		return $row;
	}
	function get_token($info){
		
		$last_login = time();
		$this->edit(array('last_login'=>$last_login),"id='{$info['id']}'");
		
		$source = $info['id'] .'|'. $info['username'] .'|'. md5($info['password']) .'|'. $last_login;
		$tt = $this->lock_url($source,'123456');
		$base = base64_encode($tt);
		
		return $base;
	}
	
	//openid获取token
	function token_get_user($token,$totakn_time=0){

		$base = base64_decode($token);
		$source = $this->unlock_url($base,'123456');

		$fg  = explode('|',$source);
		if(count($fg)!=4){
			return false;
		}
		$arr = array(
			'id'=>$fg[0],
			'username'=>$fg[1],
			'password'=>$fg[2],
			'last_login'=>$fg[3],
		);
		
		$time = time();
		
		//判断时间大于1小时不操作就退出登录
		if($totakn_time>0 && $time>$totakn_time+3600){
			return false;
		}
		//var_dump(date('Y-m-d H:i:s',$totakn_time),date('Y-m-d H:i:s'));exit;
		
		
		$user = $this->get_info($arr['id']);
		if(!$user){
			return false;
		}
		
		if(!($arr['username'] == $user['username'] && $arr['password'] == md5($user['password']) && $arr['last_login'] == $user['last_login'])){
			return false;
		}
		if($user['username']!='admin' && $user['is_enable']==0){
			return false;
		}
		
		return $user;
	}
	
	//加密函数
	function lock_url($txt,$key='slinw'){
	  if(empty($txt)){
		return '';
	  }
	  $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
	  $nh = rand(0,64);
	  $ch = $chars[$nh];
	  $mdKey = md5($key.$ch);
	  $mdKey = substr($mdKey,$nh%8, $nh%8+7);
	  $txt = base64_encode($txt);
	  $tmp = '';
	  $i=0;$j=0;$k = 0;
	  for ($i=0; $i<strlen($txt); $i++) {
		$k = $k == strlen($mdKey) ? 0 : $k;
		$j = ($nh+strpos($chars,$txt[$i])+ord($mdKey[$k++]))%64;
		$tmp .= $chars[$j];
	  }
	  return urlencode($ch.$tmp);
	}
	//解密函数
	function unlock_url($txt,$key='slinw'){
	  if(empty($txt)){
		return '';
	  }
	  $txt = urldecode($txt);
	  $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
	  $ch = $txt[0];
	  $nh = strpos($chars,$ch);
	  $mdKey = md5($key.$ch);
	  $mdKey = substr($mdKey,$nh%8, $nh%8+7);
	  $txt = substr($txt,1);
	  $tmp = '';
	  $i=0;$j=0; $k = 0;
	  for ($i=0; $i<strlen($txt); $i++) {
		$k = $k == strlen($mdKey) ? 0 : $k;
		$j = strpos($chars,$txt[$i])-$nh - ord($mdKey[$k++]);
		while ($j<0) $j+=64;
		$tmp .= $chars[$j];
	  }
	  return base64_decode($tmp);
	}
	
	//账户操作
	function account_act($user_id,$val,$type=0,$desc=''){
		$info = $this->get_info($user_id);
		if(!$info){
			return false;
		}
		
		$time = time();
		if($type===0){
			$num = $info['money'];
			$num += $val;
			if($num<0){
				return false;
			}
			$this->edit(array('money'=>$num),"id='{$user_id}'");
		}
		else{
			$num = $info['integral'];
			$num += $val;
			if($num<0){
				return false;
			}
			$this->edit(array('integral'=>$num),"id='{$user_id}'");
		}
		
		$this->load->model('Index/AccountLogModel','acclog');
		//log
		$arr = array(
			'user_id'=>$user_id,
			'type'=>$type,
			'val'=>$val,
			'info'=>$desc,
			'addtime'=>$time
		);
		$this->acclog->add($arr);
		return true;
	}
	
	
}

